package cn.main.web;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Properties;
import java.util.UUID;

/**
 * Author: chengwei
 * Date:   2016/5/10
 * Time:   17:15
 */
public class WebListener implements ServletContextListener {

    protected static final Logger log = Logger.getLogger(WebListener.class);
    @Override
    public void contextInitialized(ServletContextEvent event) {
        log.debug("开始加载配置...");
        WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        JdbcTemplate jt = app.getBean(JdbcTemplate.class);
        ConfigLoad config = new ConfigLoad(jt);
        Properties properties = config.init();
        
        event.getServletContext().setAttribute("config",properties);

        //配置信息的版本
        String config_version = UUID.randomUUID().toString().replace("-","");
        event.getServletContext().setAttribute("config_version",config_version);
        log.info("当前系统配置版本ID:" + config_version);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        event.getServletContext().removeAttribute("config");
    }
}
